<?php
/*
	本插件：$this->model('model')->方法名();
	其他地方调用：model('jy_kaoshi/model')->方法名();
*/
namespace jy_xiangqin;
defined('BY_JYA') or exit('error');
class ping extends \plugin{
	public function checkping(){
		$ks_setting=$this->plugin_setting('ks_setting');
		if(!$ks_setting['is_open'] || !$ks_setting['allscore']){
			return true;
		}
		$canyu=pdo_get('jy_xiangqin_pingdatilog',array('issub'=>1,'uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid']),'','score desc');
		$maxscore=$canyu['score'];
		if($ks_setting['allscore']>$maxscore){
			exi('请前往答题，满分后才能参与【约瓶】！','error','jy_xiangqin/ping.timu/');
		}
		return true;
	}
	public function getscore($content){
		$score=0;
		$allscore=0;
		$danan_key=$this->danan_key;
		foreach($content as &$item){
			$allscore +=$item['score'];
			if($item['type']==4){
				if($danan_key[$item['data']]==$item['daan']){
					$score +=$item['score'];
					$item['getscore']=$item['score'];
				}else{
					$item['nofen']=1;
				}
			}elseif($item['type']==5){
				$item['data']=str_array($item['data']);
				$zhong=0;
				foreach($item['data'] as $dv){
					$daan=str_replace('|',';',$item['daan']);
					$daan=';'.$daan.';';
					if(strpos($daan,';'.$danan_key[$dv].';') !== false){
						$zhong++;
					}else{
						$item['nofen']=1;
						break;
					}
				}
				if(!$item['nofen']){
					$item['daan']=str_array($item['daan']);
					$fen=intval($item['score']*$zhong/count($item['daan']));
					$item['getscore']=$fen;
					$score +=$fen;
				}
			}elseif($item['type']==20){
				if($danan_key[$item['data']]==$item['daan']){
					$score +=$item['score'];
					$item['getscore']=$item['score'];
				}else{
					$item['nofen']=1;
				}
			}
		}
		$res=array('score'=>$score,'allscore'=>$allscore,'content'=>$content);
		return $res;
	}
	public function getonescore($item){
			if($item['type']==4){
				if($item['data']==$item['daan']-1){
					$score=$item['score'];
				}else{
					$item['nofen']=1;
				}
			}elseif($item['type']==5){
				$item['data']=str_array($item['data']);
				$zhong=0;
				foreach($item['data'] as $dv){
					$dv=$dv+1;
					if(strpos($item['daan'],';'.$dv.';') !== false){
						$zhong++;
					}else{
						$item['nofen']=1;
						break;
					}
				}
				if(!$item['nofen']){
					$item['daan']=str_array($item['daan']);
					$fen=intval($item['score']*$zhong/count($item['daan']));
					$score=$fen;
				}
			}elseif($item['type']==20){
				if($item['data']==$item['daan']){
					$score=$item['score'];
				}else{
					$item['nofen']=1;
				}
			}
			$item['getscore']=$score;
			$res=array('score'=>$score,'item'=>$item);
			return $res;
	}
	public function gettikus($item=array()){
		$params=array('uniacid'=>$_SESSION['uniacid'],'status'=>1);
		if($item['keywords']){
			$params['title like']='%'.trim($item['keywords']).'%';
			$return['params']['keywords']=$item['keywords'];
		}
		$tikus=array();
		$filedata=array('id','title','desc','score','type','daan','xuanxiang','images');
		if($item['typenum']['type4']){
			$params['type']=4;
			$type4alltiku_ids=pdo_getall('jy_xiangqin_pingtimu',$params,$filedata);
			$type4allnum=count($type4alltiku_ids);
			if($type4allnum){
				$typenum=$item['typenum']['type4']<$type4allnum?$item['typenum']['type4']:$type4allnum;
				for($i=1;$i<=$typenum;$i++){
					$nowkey=rand(0,$type4allnum-$i);
					$tikus[]=$type4alltiku_ids[$nowkey];
					unset($type4alltiku_ids[$nowkey]);
					$type4alltiku_ids=array_values($type4alltiku_ids);
				}
			}
		}
		if($item['typenum']['type5']){
			$params['type']=5;
			$type5alltiku_ids=pdo_getall('jy_xiangqin_pingtimu',$params,$filedata);
			$type5allnum=count($type5alltiku_ids);
			if($type5allnum){
				$typenum=$item['typenum']['type4']<$type5allnum?$item['typenum']['type5']:$type5allnum;
				for($i=1;$i<=$typenum;$i++){
					$nowkey=rand(0,$type5allnum-$i);
					$tikus[]=$type5alltiku_ids[$nowkey];
					unset($type5alltiku_ids[$nowkey]);
					$type5alltiku_ids=array_values($type5alltiku_ids);
				}
			}
		}
		if($item['typenum']['type20']){
			$params['type']=20;
			$type20alltiku_ids=pdo_getall('jy_xiangqin_pingtimu',$params,$filedata);
			$type20allnum=count($type20alltiku_ids);
			if($type20allnum){
				$typenum=$item['typenum']['type20']<$type4allnum?$item['typenum']['type20']:$type20allnum;
				for($i=1;$i<=$typenum;$i++){
					$nowkey=rand(0,$type20allnum-$i);
					$tikus[]=$type20alltiku_ids[$nowkey];
					unset($type20alltiku_ids[$nowkey]);
					$type20alltiku_ids=array_values($type20alltiku_ids);
				}
			}
		}
		foreach($tikus as &$iv){
			$iv['xuanxiang']=json_decode($iv['xuanxiang'],true);
			$iv['images']=json_decode($iv['images'],true);
		}
		return $tikus;
	}
	public function get_csvitems($rows){
			$rows=array_values($rows);
			$num = count($rows);
			$i = 0;
			$colsIndex = $rows[0];
			$rows = array_slice($rows, 2, count($rows) - 2);
			$items = array();
			$num = 0;
			foreach ($rows as $rownu => $rv) {
				$item = array();
				foreach ($colsIndex as $cols => $cv) {
						$item[$cv]=$rv[$cols];
				}
				$items[] = $item;
				++$num;
			}
			return $items;
	}
	public function down_csv($params=array())
	{
		$list=pdo_getall('jy_xiangqin_pingtimu',$params);
		foreach($list as &$rv){
			if($rv['type']==4){
				$rv['type']='单选题';
			}
			if($rv['type']==5){
				$rv['type']='多选题';
			}
			if($rv['type']==20){
				$rv['type']='判断题';
			}
			$value=json_decode($rv['xuanxiang'],true);
			$rv['value']='';
			foreach($value as $vv){
				if($rv['value']){
					$rv['value'] .='|'.$vv['title'];
				}else{
					$rv['value'] .=$vv['title'];
				}
			}
		}
		$header=array(
				'title'=>'题目标题',
				'type'=>'题目类型',
				'score'=>'题目分数',
				'daan'=>'题目答案',
				'value'=>'选项内容',
				'desc'=>'题目解析',
		);
		cfc('tools')->export2excel($header,$list,'[题库列表]',true);
	}
	//导出试卷
	public function down_canyu($id)
	{
		$canyu=pdo_get('jy_xiangqin_pingdatilog',array('id'=>$id));
		$list=json_decode($canyu['content'],true);
		foreach($list as &$rv){
			if($rv['type']==4){
				$rv['type']='单选题';
				$rv['data']=$this->danan_key[$rv['data']];
			}
			if($rv['type']==5){
				$rv['type']='多选题';
			}
			if($rv['type']==20){
				$rv['type']='判断题';
				$rv['data']=$this->danan_key[$rv['data']];
			}
			$value=$rv['xuanxiang'];
			$rv['value']='';
			foreach($value as $vv){
				if($rv['value']){
					$rv['value'] .='|'.$vv['title'];
				}else{
					$rv['value'] .=$vv['title'];
				}
			}
			
		}
		$header=array(
				'title'=>'题目标题',
				'type'=>'题目类型',
				'score'=>'题目分数',
				'data'=>'答题答案',
				'daan'=>'正确答案',
				'value'=>'选项内容',
				'desc'=>'题目解析',
		);
		cfc('tools')->export2excel($header,$list,'[答卷]',true);
	}
	public function add_canyu($params=array()){
		$insert=array(
			'uniacid'=>$_SESSION['uniacid'],
			'mid'=>$_SESSION['uid'],
			'datiid'=>$params['datiid'],
			'score'=>0,
			'allscore'=>$params['allscore'],
			'dayid'=>date('Y-m-d',TIMESTAMP),
			'content'=>ijson_encode($params['content']),
			'status'=>1,
			'createtime'=>TIMESTAMP,
		);
		pdo_insert('jy_xiangqin_pingdatilog',$insert);
		$id=pdo_insertid();
		return $id;
	}
	public function up_canyu($params=array())
	{
		$content=$params['content'];
		$update=array(
			'content'=>ijson_encode($content),
		);
		pdo_update('jy_xiangqin_pingdatilog',$update,array('id'=>$params['id'],'uniacid'=>$_SESSION['uniacid']));
		return true;
	}
	public function up_buyfee($order=array()){
		$pingset=$this->plugin_setting('ping');
		$member=$this->model('member')->get_member($order['mid']);
		if($pingset['nv_buysetopen'] && $member['sex']==2){
			$buyset=$pingset['nv_buyset'];
		}elseif($pingset['nan_buysetopen'] && $member['sex']==1){
			$buyset=$pingset['nan_buyset'];
		}
		if(!$buyset){
			return false;
		}
		paixu($buyset,'buynum','desc');
		foreach($buyset as $item){
				$item['buynum']=intval($item['buynum']);
				if($item['buynum']<=$order['num']){
					if(intval($item['sendnum'])){
						pdo_update('jy_xiangqin_member',array('pingnum +='=>intval($item['sendnum'])),array('mid'=>$order['mid'],'uniacid'=>$_SESSION['uniacid']));
					}
					if($item['jifenrate']>0){
						cfc('core')->member_credit($order['mid'],'credit1',$item['jifenrate'],true,'jy_xiangqin','单次购买满'.$item['buynum'].'个瓶子，送积分');
					}
					break;
				}
		}
		return true;
	}
}
?>